﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using OutLook.Model.Domains;
using System.IO;
using System.Xml;
using OutLook.Model.Infrastructure;
using OutLook.UI.WPFApp.ResourceInformation;
using System.Data;
using System.Windows.Controls.DataVisualization.Charting;
using OutLook.Module.Charting;
using OutLook.Module.Infrastructure;
using OutLook.Module.Load.Contracts;
using OutLook.UI.WPFApp.Helpers;
using OutLook.Module.Load;

namespace OutLook.UI.WPFApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            ApplicationCulture.SetCulture();
            //BitmapImage bi = new BitmapImage();
            //bi.BeginInit();
            //bi.UriSource = ApplicationCulture.PictureSource;
            //bi.EndInit();
            //LocalizationImg.Source = bi;

            InitializeComponent();

            m_OpenDialog.Filter = Constants.DIALOG_FILTER;
            m_OpenDialog.Title = Constants.OPEN_DIALOG_TITLE;
            m_SaveDialog.Filter = Constants.DIALOG_FILTER;
            m_SaveDialog.Title = Constants.SAVE_DIALOG_TITLE;

            InitializeEnthalpyChart();
            InitializeMetalSlagChart();
            InitializeSlagPropertiesChart();

            lbl_curUser.Content = string.Format("{0}{1} {2}. {3}", ResourceInformation.MainPage.Text_wellcome,
                                                               LoadPackage.Instance.GetCurrentUser().FirstName,
                                                               LoadPackage.Instance.GetCurrentUser().LastName,
                                                               ResourceInformation.MainPage.Text_enter);
        }

        private void ImgLoad_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (m_OpenDialog.ShowDialog().Value)
            {
                LoadExpander.IsExpanded = true;
                DataTable dataTable = LoadPackage.Instance.DownloadTheLoad(m_OpenDialog.FileName);
                dataTable.DefaultView.AllowNew = false;
                dataTable.DefaultView.AllowDelete = false;
                DataGridLoad.ItemsSource = dataTable.DefaultView;
                DataGridLoad.AutoGenerateColumns = false;
                for (int i = 0; i < dataTable.Columns.Count; i++)
                {
                    DataGridTextColumn TC = new DataGridTextColumn();
                    TC.Binding = new Binding(dataTable.Columns[i].ColumnName);
                    TC.Header = dataTable.Columns[i].ColumnName;
                    TC.Width = 80;
                    DataGridLoad.Columns.Add(TC);
                }
                DataGridLoad.Columns[0].Width = 35;
                DataGridLoad.Columns[1].Width = 175;
                DataGridLoad.Columns[2].Width = 120;

                DataTable dataTableIndicators = LoadPackage.Instance.ChargeIndicators();
                dataTableIndicators.DefaultView.AllowNew = false;
                dataTableIndicators.DefaultView.AllowDelete = false;
                dataTableIndicators.DefaultView.AllowEdit = false;
                DataGridChemicalComposition.ItemsSource = dataTableIndicators.DefaultView;
                DataGridChemicalComposition.AutoGenerateColumns = false;
                for (int i = 0; i < dataTableIndicators.Columns.Count; i++)
                {
                    DataGridTextColumn TC = new DataGridTextColumn();
                    TC.Binding = new Binding(dataTableIndicators.Columns[i].ColumnName);
                    TC.Header = dataTableIndicators.Columns[i].ColumnName;
                    TC.Width = 80;
                    DataGridChemicalComposition.Columns.Add(TC);
                }
                DataGridChemicalComposition.Columns[0].Width = 100;

                DataTable dataTableProperties = LoadPackage.Instance.ChargeProperties();
                dataTableProperties.DefaultView.AllowNew = false;
                dataTableProperties.DefaultView.AllowDelete = false;
                dataTableProperties.DefaultView.AllowEdit = false;
                DataGridChargeProperties.ItemsSource = dataTableProperties.DefaultView;
                DataGridChargeProperties.AutoGenerateColumns = false;
                DataGridChargeProperties.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
                for (int i = 0; i < dataTableProperties.Columns.Count; i++)
                {
                    DataGridTextColumn TC = new DataGridTextColumn();
                    TC.Binding = new Binding(dataTableProperties.Columns[i].ColumnName);
                    TC.Header = dataTableProperties.Columns[i].ColumnName;
                    TC.Width = 120;
                    DataGridChargeProperties.Columns.Add(TC);
                }


                DataTable dataTableTechnology = LoadPackage.Instance.DownloadTheTechnology(LoadPackage.Instance.CurrentLoadFileName);
                if (dataTableTechnology == null)
                    MessageBox.Show("Загрузить технологию не удалось!", "ПРЕДУПРЕЖДЕНИЕ");
                else
                {
                    dataTableTechnology.DefaultView.AllowNew = false;
                    dataTableTechnology.DefaultView.AllowDelete = false;
                    dataTableTechnology.DefaultView.AllowEdit = false;
                    dataGrid_TechnologyParameters.ItemsSource = dataTableTechnology.DefaultView;
                    dataGrid_TechnologyParameters.AutoGenerateColumns = false;
                    dataGrid_TechnologyParameters.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
                    for (int i = 0; i < dataTableTechnology.Columns.Count; i++)
                    {
                        DataGridTextColumn TC = new DataGridTextColumn();
                        TC.Binding = new Binding(dataTableTechnology.Columns[i].ColumnName);
                        TC.Header = dataTableTechnology.Columns[i].ColumnName;
                        dataGrid_TechnologyParameters.Columns.Add(TC);
                    }
                    dataGrid_TechnologyParameters.Columns[0].Width = 35;
                    dataGrid_TechnologyParameters.Columns[1].Width = 350;
                    dataGrid_TechnologyParameters.Columns[2].Width = 120;
                }

                LoadPackage.Instance.SetOutput();

                DataTable dataTableIronCast = LoadPackage.Instance.GetCastIron();
                dataTableIronCast.DefaultView.AllowNew = false;
                dataTableIronCast.DefaultView.AllowDelete = false;
                dataTableIronCast.DefaultView.AllowEdit = false;
                dataGrid_IronCast.ItemsSource = dataTableIronCast.DefaultView;
                dataGrid_IronCast.AutoGenerateColumns = false;
                for (int i = 0; i < dataTableIronCast.Columns.Count; i++)
                {
                    DataGridTextColumn TC = new DataGridTextColumn();
                    TC.Binding = new Binding(dataTableIronCast.Columns[i].ColumnName);
                    TC.Header = dataTableIronCast.Columns[i].ColumnName;
                    TC.Width = 120;
                    dataGrid_IronCast.Columns.Add(TC);
                }

                DataTable dataTableISlag = LoadPackage.Instance.GetSlag();
                dataTableISlag.DefaultView.AllowNew = false;
                dataTableISlag.DefaultView.AllowDelete = false;
                dataTableISlag.DefaultView.AllowEdit = false;
                dataGrid_Slag.ItemsSource = dataTableISlag.DefaultView;
                dataGrid_Slag.AutoGenerateColumns = false;
                for (int i = 0; i < dataTableISlag.Columns.Count; i++)
                {
                    DataGridTextColumn TC = new DataGridTextColumn();
                    TC.Binding = new Binding(dataTableISlag.Columns[i].ColumnName);
                    TC.Header = dataTableISlag.Columns[i].ColumnName;
                    TC.Width = 120;
                    dataGrid_Slag.Columns.Add(TC);
                }

            }
        }

        private void ChartsExpander_Expanded(object sender, RoutedEventArgs e)
        {

        }

        #region Charts Initialization

        private void InitializeEnthalpyChart()
        {
            ((LineSeries)ChartEntalpiya.Series[0]).ItemsSource = ChartController.GetEnthalpyData();
            ((LineSeries)ChartEntalpiya.Series[0]).DataPointStyle = GetNewDataPointStyle(Color.FromRgb(0, 0, 139));

            ((LineSeries)ChartEntalpiya.Series[1]).ItemsSource = new KeyValuePair<double, double>[]{
                new KeyValuePair<double,double>(ChartCharacteristics.DEL, 1800),
                ChartController.GetPointOnChartEnthalpy(ChartCharacteristics.DEL)
            };
            ((LineSeries)ChartEntalpiya.Series[1]).DataPointStyle = GetNewDataPointStyle(Color.FromRgb(176, 196, 222));

            ((LineSeries)ChartEntalpiya.Series[2]).ItemsSource = new KeyValuePair<double, double>[]{
                new KeyValuePair<double,double>(ChartCharacteristics.DEP, 1800),
                ChartController.GetPointOnChartEnthalpy(ChartCharacteristics.DEP)
            };
            ((LineSeries)ChartEntalpiya.Series[2]).DataPointStyle = GetNewDataPointStyle(Color.FromRgb(176, 196, 222));


            ((ScatterSeries)ChartEntalpiya.Series[3]).ItemsSource = new KeyValuePair<double, double>[]{
                new KeyValuePair<double,double>(ChartCharacteristics.DEP, 1800),
                ChartController.GetPointOnChartEnthalpy(ChartCharacteristics.DEP),
                new KeyValuePair<double,double>(ChartCharacteristics.DEL, 1800),
                ChartController.GetPointOnChartEnthalpy(ChartCharacteristics.DEL)
            };
        }

        private void InitializeMetalSlagChart()
        {
            ((LineSeries)ChartMetalSlag.Series[0]).ItemsSource = ChartController.GetMetalSlagData();
            ((LineSeries)ChartMetalSlag.Series[0]).DataPointStyle = GetNewDataPointStyle(Color.FromRgb(255, 108, 0));
        }

        private void InitializeSlagPropertiesChart()
        {
            ((LineSeries)ChartSlagProperties.Series[0]).ItemsSource = ChartController.GetSlagPropertiesY1Data();
            ((LineSeries)ChartSlagProperties.Series[0]).DataPointStyle = GetNewDataPointStyle(Color.FromRgb(255, 0, 0));
            ((LineSeries)ChartSlagProperties.Series[1]).ItemsSource = ChartController.GetSlagPropertiesY2Data();
            ((LineSeries)ChartSlagProperties.Series[1]).DataPointStyle = GetNewDataPointStyle(Color.FromRgb(0, 255, 0));
        }

        /// <summary>
        /// Gets the new data point style.
        /// </summary>
        /// <returns></returns>
        private Style GetNewDataPointStyle(Color color)
        {
            Color background = color;
            Style style = new Style(typeof(DataPoint));
            Setter st1 = new Setter(DataPoint.BackgroundProperty,
                                        new SolidColorBrush(background));
            Setter st2 = new Setter(DataPoint.BorderBrushProperty,
                                        new SolidColorBrush(Colors.White));
            Setter st3 = new Setter(DataPoint.BorderThicknessProperty, new Thickness(0.1));

            Setter st4 = new Setter(DataPoint.TemplateProperty, null);
            style.Setters.Add(st1);
            style.Setters.Add(st2);
            style.Setters.Add(st3);
            style.Setters.Add(st4);
            return style;
        }

        #endregion

        #region Fields

        private OpenFileDialog m_OpenDialog = new OpenFileDialog();
        private SaveFileDialog m_SaveDialog = new SaveFileDialog();
        private bool m_SwithchCulture = false;

        #endregion

        private void LocalizationImg_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {

        }

        private void LocalizationImg_MouseDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void LocalizationImg_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (m_SwithchCulture)
                ApplicationCulture.Culture = ApplicationCulture.RU;

            ApplicationCulture.Culture = ApplicationCulture.EN;
            ApplicationCulture.SetCulture();

            m_SwithchCulture = !m_SwithchCulture;

            InitializeComponent();
            
        }

        private void ImgConfig_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {

        }

        private void ImgLoad_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {

        }

        private void BtnCalculate_Click(object sender, RoutedEventArgs e)
        {

        }

    }
}
